import Vue from 'vue'
import VueRouter from 'vue-router'
const Home = () => import('../components/Home.vue')
const Login = () => import('../components/Login.vue')
const Welcome = () => import('../components/Welcome.vue')
const Caselist = () => import('../components/Caselist.vue')
const Order = () => import('../components/Order.vue')
const Event = () => import('../components/Event.vue')
const Cate = () => import('../components/Cate.vue')
const Company = () => import('../components/Company.vue')
const Article = () => import('../components/Article.vue')
const User = () => import('../components/User.vue')
const AddCase = () => import('../components/AddCase.vue')
const UpdateCase = () => import('../components/UpdateCase.vue')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login,
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/caselist', component: Caselist },
      { path: '/order', component: Order },
      { path: '/event', component: Event },
      { path: '/cate', component: Cate },
      { path: '/company', component: Company },
      { path: '/article', component: Article },
      { path: '/user', component: User },
      { path: '/addCase', component: AddCase },
      { path: '/updateCase/:id', component: UpdateCase },
    ],
  },
]

const router = new VueRouter({
  routes,
})

//避免重复点击当前页面的路径提示异常，不影响程序的正常执行
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err)
}

function getTitle(path) {
  if (path.indexOf('updateCase') !== -1) {
    return '修改案例'
  }
  switch (path) {
    case '/home': {
      return '管理首页'
    }
    case '/': {
      return '登录页面'
    }
    case '/case': {
      return '案例管理'
    }
    case '/article': {
      return '文章管理'
    }
    case '/order': {
      return '预约管理'
    }
    case '/event': {
      return '活动管理'
    }
    case '/caselist': {
      return '案例列表'
    }
    case '/company': {
      return '公司信息'
    }
    case '/cate': {
      return '分类管理'
    }
    case '/user': {
      return '用户管理'
    }
    case '/welcome': {
      return '欢迎'
    }
    case '/addCase': {
      return '添加案例'
    }
  }
}

router.beforeEach((to, from, next) => {
  console.log('from:' + from.path)
  document.title = getTitle(to.path)
  //1、拦截登录
  if (to.path === '/') {
    return next()
  }
  //2、获得登录信息
  let str = sessionStorage.getItem('login_user')
  if (!str) {
    return next('/')
  }
  let user = JSON.parse(str)
  if (!user.data) {
    return next('/')
  }
  if (!user.data.user) {
    return next('/')
  }
  if (!user.data.user.name) {
    return next('/')
  }
  //放行
  next()
})

export default router

/*
  route:是定义在路由文件中的一个对象，包含路径和组件之间的对应关系
  routes:在路由文件中的一个数组，存放很多个route
  $route:当前组件对应的url对象 http://localhost:9999/#/home就是home.vue的$route
  router:在路由文件中定义的VueRouter类型的对象，称路由器，其中包含很多route
  $router:是全局Vue对象的一个属性，可以通过this.$router来获得到router
*/
